import localStorage from '@/utils/localStorage'
import Vue from 'vue'
import { get, post, del } from '@/utils/request'

export function login(appId, apk, realId) {
  let uuid = require('uuid')
  if (realId != '') {
    localStorage.setLocalStorage('realId', realId)
  } else {
    localStorage.delLocalStorage('realId')
  }
  let ru = location.origin + location.pathname + encodeURIComponent('#/login_h4a')
  let url =
    global.H4a +
    '/SsoLogin.aspx?aid=' +
    appId +
    '&pm=FormsAuthentication&apk=' +
    apk +
    '&t=token&lou=' +
    encodeURIComponent(location.origin + location.pathname + encodeURIComponent('#/loginOut')) +
    '&ip=' +
    window.location.hostname +
    '&fciam=False&iam=forms&fc=False&sid=' +
    uuid.v4() +
    '&ru=' +
    encodeURIComponent(ru)
  window.location.replace(url)
}
/** 登出 **/
export function logout(callbackUrl) {
  let ru = callbackUrl ? encodeURIComponent(callbackUrl) : location.origin + location.pathname + encodeURIComponent('#/login_h4a')
  window.location.replace(global.H4a + '/LogOff.aspx?lar=True&ru=' + ru)
}

export async function saveAudit(queryType, isSuccess, errorInfo, H4aToken) {
  await post(
    {
      url:
        global.HUNTERHOST +
        `/hbAudit/saveAudit?operateType=` +
        queryType +
        '&flag=' +
        isSuccess +
        '&extTime=1000&errorCode&errorInfo=' +
        errorInfo +
        '&token=' +
        H4aToken,
      headers: {
        'Hunter-User-Origin': 'H4A'
      }
    },
    {}
  ).catch(e => {
    console.error(e)
  })
  return null
}
